package com.pureut.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.domain.SupplierElimination;
import com.pureut.system.domain.SupplierList;
import com.pureut.system.domain.SupplierPerformance;
import com.pureut.system.domain.dto.SupplierPerformancePagesDto;
import com.pureut.system.domain.vo.SupplierPerformanceVo;
import com.pureut.system.mapper.SupplierPerformanceMapper;
import com.pureut.system.service.ISupplierListService;
import com.pureut.system.service.ISupplierPerformanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author: pureut
 * @Date: 2024-12-05 17:16:19
 * @Desc: 供应商绩效
 */
@Service("supplierPerformanceService")
public class SupplierPerformanceServiceImpl extends ServiceImpl<SupplierPerformanceMapper, SupplierPerformance> implements ISupplierPerformanceService {

    /**
     * 供应商绩效Dao
     */
    @Resource
    private SupplierPerformanceMapper supplierPerformanceMapper;

    /**
     * 供应商名录Service
     */
    @Resource
    private ISupplierListService supplierListService;

    @Resource
    FeignService feignService;

    /**
     * 查询列表
     * @param supplierPerformanceVo
     * @return
     */
    @Override
    public List<SupplierPerformancePagesDto> queryPage(SupplierPerformanceVo supplierPerformanceVo) {
        // 获取认证方式的数据字典
        List<SysDictData> netArray = DictUtils.getDictCache("supplier_type");
        Map<String, String> netMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<SysDictData> netArray2 = DictUtils.getDictCache("transaction_type");
        Map<String, String> netMap2 = netArray2.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        List<SupplierPerformancePagesDto> supplierPerformancePagesDtos = supplierPerformanceMapper.selectByPage(supplierPerformanceVo);
        supplierPerformancePagesDtos.forEach(e->{
            e.setSupplierCategory(netMap.get(String.valueOf(e.getSupplierCategory())));
            e.setTransactionType(netMap2.get(String.valueOf(e.getTransactionType())));

        });
        return supplierPerformancePagesDtos;
    }


    /**
     * 新增供应商绩效
     * @param supplierPerformanceVo
     * @return
     */
    @Override
    public Boolean save(SupplierPerformanceVo supplierPerformanceVo) {
        SupplierPerformance supplierPerformance = new SupplierPerformance();
        BeanUtil.copyProperties(supplierPerformanceVo, supplierPerformance);
        String sn = feignService.getAuthorityCoding("warehouse:outbound:reworksto:list");
        if (sn == null) {
            throw new GlobalException("该单据未进行配置,无法生成单据编码");
        }
        supplierPerformance.setDocNum(sn);
        supplierPerformance.setCreateBy(SecurityUtils.getUsername());
        supplierPerformance.setCreateTime(new Date());
        supplierPerformance.setStatus("1");
        supplierPerformance.setAggregateScore(supplierPerformanceVo.getCostScore()*supplierPerformanceVo.getCostWeight()+supplierPerformanceVo.getCommercialWeight()*supplierPerformanceVo.getCommercialScore()+supplierPerformanceVo.getDeliveryWeight()*supplierPerformanceVo.getDeliveryScore()+supplierPerformanceVo.getQualityAfterSaleScore()*supplierPerformanceVo.getQualityAfterSaleWeight());
        return save(supplierPerformance);
    }


    /**
     * 更新供应商绩效
     * @param supplierPerformanceVo
     * @return
     */
    @Override
    public Boolean updateById(SupplierPerformanceVo supplierPerformanceVo) {
        SupplierPerformance supplierPerformance = new SupplierPerformance();
        BeanUtil.copyProperties(supplierPerformanceVo, supplierPerformance);
        supplierPerformance.setUpdateBy(SecurityUtils.getUsername());
        supplierPerformance.setUpdateTime(new Date());
        return updateById(supplierPerformance);
    }

    @Override
    public Boolean submit(Long id){
        SupplierPerformance supplierAccess = new SupplierPerformance();
        supplierAccess.setId(id);
        supplierAccess.setStatus("2");
        return updateById(supplierAccess);
    }

    @Transactional
    @Override
    public Boolean examine(SupplierPerformanceVo supplierPerformanceVo){
        SupplierPerformance supplierAccess = getById(supplierPerformanceVo.getId());
        supplierAccess.setId(supplierPerformanceVo.getId());
        if(supplierPerformanceVo.getAuditState()==1){
            supplierAccess.setStatus("3");
        }else{
            supplierAccess.setStatus("4");
        }
        //        supplierAccess.set(SecurityUtils.getUsername());
//        supplierAccess.setApprovalTime(new Date());
        boolean b = updateById(supplierAccess);
        if(b&&("3").equals(supplierAccess.getStatus())){
            String s = determineGrade(supplierAccess.getAggregateScore().intValue());
            SupplierList supplierList = new SupplierList();
            supplierList.setId(supplierAccess.getSupplierId());
            supplierList.setSupplierLevel(s);
            supplierList.setAggregateScore(supplierAccess.getAggregateScore());
            supplierListService.updateById(supplierList);
        }
        return b;
    }

    // 方法：根据得分判断供应商等级
    public static String determineGrade(int score) {
        if (score > 90 && score < 100) {
            return "1";
        } else if (score > 80 && score <= 90) {
            return "2";
        } else if (score > 70 && score <= 80) {
            return "3";
        } else if (score <= 70) {
            return "4";
        } else {
            // 处理非法得分情况，比如负数等
            return "4";
        }
    }


}
